package com.msr.controller;

import com.msr.bean.User;
import com.msr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author tom
 * @version V1.0
 * @Package com.msr.controller
 * @date 2024/3/13 10:00
 * @Copyright © 株式会社多言语系统研究所
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 跳转到登录页面
     * @param user
     * @return
     */
    @GetMapping("/toLogin")
    public String toLoginPage(User user){
        return "login";
    }

    /**
     * 退出登录
     * @param user
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";//跳转到登录页面
    }

    /**
     * 处理登录业务
     * @param user
     * @return
     */
    @PostMapping("/user_login")
    public String user_login(User user, HttpSession session){
        User u = userService.login(user);
        if(u!=null){
            //登录成功：跳转到index页面,保存登录的用户对象
            session.setAttribute("loginUser",u);
            return "index";
        }else{
            //登录失败:跳转login页面
            return "login";
        }
    }
}
